package cn.springboot.erp.service.impl;

import cn.springboot.erp.commons.QueryParams;
import cn.springboot.erp.commons.ResponseResult;
import cn.springboot.erp.entity.Categories;
import cn.springboot.erp.mapper.CategoriesMapper;
import cn.springboot.erp.vo.ProductVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.springboot.erp.entity.Products;
import cn.springboot.erp.service.ProductsService;
import cn.springboot.erp.mapper.ProductsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author asus
* @description 针对表【products】的数据库操作Service实现
* @createDate 2024-05-11 11:56:02
*/
@Service
public class ProductsServiceImpl extends ServiceImpl<ProductsMapper, Products>
    implements ProductsService{

    @Resource
    private CategoriesMapper categoriesMapper;
    @Override
    public ResponseResult listPage(QueryParams<Products> queryParams) {
        // 获取分页参数
        Integer page = queryParams.getPage();
        Integer limit = queryParams.getLimit();
        Products searchParams = queryParams.getSearchParams();
        // 分页
        Page<Products> productsPage = new Page<>(page, limit);
        LambdaQueryWrapper<Products> lambda = new QueryWrapper<Products>().lambda();
        lambda.like(StringUtils.isNotBlank(searchParams.getName()),Products::getName,searchParams.getName())
                .eq(ObjectUtils.isNotNull(searchParams.getCategoryId()),Products::getCategoryId,searchParams.getCategoryId())
                .orderByDesc(Products::getCreateTime);
        baseMapper.selectPage(productsPage,lambda);
        List<ProductVO> productList = productsPage.getRecords().stream().map(product -> {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product, productVO);

            // 根据商品分类id查询
            LambdaQueryWrapper<Categories> categoriesLambda = new QueryWrapper<Categories>().lambda();
            categoriesLambda.eq(Categories::getId,product.getCategoryId());
            Categories categories = categoriesMapper.selectOne(categoriesLambda);
            productVO.setCategoryName(categories.getName());
            return productVO;
        }).collect(Collectors.toList());
        return ResponseResult.Success()
                .data("total",productsPage.getTotal())
                .data("productList",productList);
    }

    @Override
    public ResponseResult categoriesList() {
        LambdaQueryWrapper<Categories> lambda = new QueryWrapper<Categories>().lambda();
        lambda.select(Categories::getId,Categories::getName);
        List<Categories> categoriesList = categoriesMapper.selectList(lambda);

        return ResponseResult.Success().data("categoriesList",categoriesList);
    }

    @Override
    public ResponseResult selectById(Integer id) {
        // 查询商品信息
        Products products = baseMapper.selectById(id);
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(products,productVO);
        // 根据商品类型id查询
        Categories categories = categoriesMapper.selectById(products.getCategoryId());
        productVO.setCategoryName(categories.getName());
        return ResponseResult.Success().data("product",productVO);
    }
}




